package com.cloud.edu.subject.controller;

import io.swagger.annotations.Api;

import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.edu.bus.entity.SubjectEntity;
import com.cloud.edu.contants.UrlConstant;
import com.cloud.edu.subject.model.UserQuestions;
import com.cloud.edu.subject.service.UserQuestionsService;
import com.cloud.edu.utils.PageUtils;
import com.cloud.edu.utils.R;

/**
 * 用户题库题目信息
 * 
 * @author dehuisun
 * @date 2018-12-12
 */
@RestController
@RequestMapping(UrlConstant.WEB_SUBJECT_URL+"/ques")
@Api(value = "UserQuestionsController", description="试题接口")
public class UserQuestionsController {
	
	@Autowired
	private UserQuestionsService userQuestionsService;
	
	/**
	 * 分页题目列表
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = userQuestionsService.queryPage(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 分页知识点列表
	 */
	@GetMapping("/knowsList")
	public R knowsList(@RequestParam Map<String, Object> params){
		PageUtils page = userQuestionsService.queryKnowsPage(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 配置信息
	 * @throws Exception 
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception{
		JSONObject obj=userQuestionsService.getQueById(id);
		JSONObject userQuestions=(JSONObject) obj.get("que");
		JSONObject subjectEntity=new JSONObject();
		if(obj.containsKey("subjectEntity")){
			subjectEntity=(JSONObject) obj.get("subjectEntity");
		}
		JSONArray knows=new JSONArray();
		if(obj.containsKey("knows")){
			knows=(JSONArray) obj.get("knows");
		}
		return R.ok().put("que", userQuestions).put("subject", subjectEntity).put("knows", knows);
	}
	
	/**
	 * 保存配置
	 * @throws Exception 
	 */
	@PostMapping("/save")
	public R save(@RequestBody JSONObject params) throws Exception{
		userQuestionsService.saveQue(params);
		return R.ok();
	}
	
	/**
	 * 修改配置
	 * @throws Exception 
	 */
	@PostMapping("/update")
	public R update(@RequestBody JSONObject params) throws Exception{
		userQuestionsService.updateQue(params);
		return R.ok();
	}
	
	/**
	 * 删除配置
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids){
		userQuestionsService.deleteBatch(ids);
		return R.ok();
	}
}
